package com.fly.demo;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.time.DateFormatUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MainRun
{
    private static ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2);
    
    private static int[] hours = {1, 2, 3, 4, 6, 8, 12, 24};
    
    /**
     * 整点倒计时
     * 
     * @param args
     * @see [类、类#方法、类#成员]
     */
    public static void main(String[] args)
    {
        long initDelay = 10000L - System.currentTimeMillis() % 10000L;
        int index = (int)System.currentTimeMillis() % hours.length;
        executorService.scheduleAtFixedRate(() -> printTime(hours[index]), initDelay, 10000, TimeUnit.MILLISECONDS);
    }
    
    private static void printTime(int hour)
    {
        // 定义时间间隔
        long between = hour * 3600000L;
        long time = between - (System.currentTimeMillis() + 8 * 3600000L) % between;
        
        // 去除100ms之内误差
        time += (100 - time % 100);
        String after = DateFormatUtils.formatUTC(time, "HH':'mm':'ss.SSS");
        log.info("--------  {} ==> {} H --------", after, hour);
    }
}
